#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@File   : log_statistics.py
@Time : 2025/3/7 9:47
@Author: Wangwc
@Describe:  ……
"""
import re
from urllib.parse import urlparse

str1 = "2025-03-06 17:11:31 | INFO | commons.logger.info: utils.requests_tools.wrapper: [method]: POST     [url]: http://172.17.21.129:9999/ugreen/v1/verify/check"
str2 = "2025-03-06 17:11:31 | INFO | commons.logger.info: utils.requests_tools.post: 发起POST请求，请求URL: http://172.17.21.129:9999/ugreen/v1/verify/check"

def extract_unique_endpoints(log_file):
    # 修正后的 URL 捕获模式
    url_pattern = re.compile(r'(?:\[url\]:|请求URL:)\s*(\S+)', re.IGNORECASE)
    endpoint_set = set()

    with open(log_file, 'r', encoding='utf-8') as f:
        for line in f:
            # 提取完整 URL
            url_match = url_pattern.search(line)
            if url_match:
                full_url = url_match.group(1)
                # 使用标准库解析 URL 路径
                parsed_path = urlparse(full_url).path

                # 路径有效性验证
                if parsed_path.startswith(('/ugreen/v1', '/ugreen/v2')):
                    # 统一去除末尾斜杠
                    clean_path = parsed_path.rstrip('/')
                    endpoint_set.add(clean_path)

    # 结果输出
    print(f"发现 {len(endpoint_set)} 个唯一接口路径")
    print("=" * 40)
    print("接口路径列表：")
    for path in sorted(endpoint_set):
        print(f"- {path}")

if __name__ == "__main__":
    extract_unique_endpoints('test_2025_03_06_171130.log')  # 替换为实际路径